package com.happy.manage.controller;

import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.happy.common.annotation.Log;
import com.happy.common.core.controller.BaseController;
import com.happy.common.core.domain.AjaxResult;
import com.happy.common.enums.BusinessType;
import com.happy.manage.domain.UserSearchTerms;
import com.happy.manage.service.IUserSearchTermsService;
import com.happy.common.utils.poi.ExcelUtil;
import com.happy.common.core.page.TableDataInfo;
import com.happy.manage.domain.HotSearchTermVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户搜索词记录Controller
 * 
 * @author ruoyi
 * @date 2024-12-28
 */
@RestController
@RequestMapping("/manage/terms")
public class UserSearchTermsController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(UserSearchTermsController.class);

    @Autowired
    private IUserSearchTermsService userSearchTermsService;

    /**
     * 查询用户搜索词记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:terms:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserSearchTerms userSearchTerms)
    {
        startPage();
        List<UserSearchTerms> list = userSearchTermsService.selectUserSearchTermsList(userSearchTerms);
        return getDataTable(list);
    }

    /**
     * 导出用户搜索词记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:terms:export')")
    @Log(title = "用户搜索词记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserSearchTerms userSearchTerms)
    {
        List<UserSearchTerms> list = userSearchTermsService.selectUserSearchTermsList(userSearchTerms);
        ExcelUtil<UserSearchTerms> util = new ExcelUtil<UserSearchTerms>(UserSearchTerms.class);
        util.exportExcel(response, list, "用户搜索词记录数据");
    }

    /**
     * 获取用户搜索词记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:terms:query')")
    @GetMapping(value = "/{searchId}")
    public AjaxResult getInfo(@PathVariable("searchId") Long searchId)
    {
        return success(userSearchTermsService.selectUserSearchTermsBySearchId(searchId));
    }

    /**
     * 新增用户搜索词记录
     */
    @PreAuthorize("@ss.hasPermi('manage:terms:add')")
    @Log(title = "用户搜索词记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserSearchTerms userSearchTerms)
    {
        return toAjax(userSearchTermsService.insertUserSearchTerms(userSearchTerms));
    }

    /**
     * 修改用户搜索词记录
     */
    @PreAuthorize("@ss.hasPermi('manage:terms:edit')")
    @Log(title = "用户搜索词记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserSearchTerms userSearchTerms)
    {
        return toAjax(userSearchTermsService.updateUserSearchTerms(userSearchTerms));
    }

    /**
     * 删除用户搜索词记录
     */
    @PreAuthorize("@ss.hasPermi('manage:terms:remove')")
    @Log(title = "用户搜索词记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{searchIds}")
    public AjaxResult remove(@PathVariable Long[] searchIds)
    {
        return toAjax(userSearchTermsService.deleteUserSearchTermsBySearchIds(searchIds));
    }

    /**
     * 获取热门搜索词列表
     */
    @PreAuthorize("@ss.hasPermi('manage:terms:list')")
    @GetMapping("/hot")
    public AjaxResult getHotSearchTerms(
        @RequestParam(required = false) 
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
        @RequestParam(required = false) 
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        try {
            if (startTime == null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                startTime = sdf.parse("2024-12-28 00:00:00");
            }
            if (endTime == null) {
                endTime = new Date();
            }
            
            if (startTime.after(endTime)) {
                return error("开始时间不能晚于结束时间");
            }
            List<HotSearchTermVO> hotTerms = userSearchTermsService.selectHotSearchTerms(startTime, endTime);
            return success(hotTerms);
        } catch (ParseException e) {
            return error("时间格式解析错误");
        } catch (Exception e) {
            return error("处理请求时发生错误：" + e.getMessage());
        }
    }
}
